<template>
  <view class="page" :style="'background-image: url(' + loginBg + ');'">
    <u-navbar
      @leftClick="navLeftClick"
      leftIcon="close"
      :fixed="false"
      title="果然年代"
      placeholder
      bgColor="transparent"
    ></u-navbar>
    <view class="u-flex-1 u-flex u-flex-column u-row-center u-col-center">
      <image style="width: 160rpx;height: 160rpx;margin-bottom: 18rpx;" src="../../static/login/logo.png" />
      <image style="width: 160rpx;height: 46rpx;margin-bottom: 184rpx;" src="../../static/login/logoTitle.png" />
      <view class="login-btn" style="background-color: #04C15F;margin-bottom: 30rpx;" @click="wxLogin">
        <image style="width: 52rpx;height: auto;margin-right: 10rpx;" src="../../static/login/weChat.png" mode="widthFix" />
        <text>微信一键登录</text>
      </view>
      <view class="login-btn" style="background-color: #FFA000;" @click="jumpTo('/pages/login/phoneLogin')">
        <image style="width: auto;height: 48rpx;margin-right: 22rpx;" src="../../static/login/phone.png" mode="heightFix" />
        <text>手机验证登录</text>
      </view>
    </view>
    <view class="agreement">
      <u-checkbox-group v-model="agreement" placement="column">
        <u-checkbox activeColor="#53be07" name="1">
        </u-checkbox>
      </u-checkbox-group>
      已阅读并同意 <text @click="jumpTo('/pages/setting/aboutUs?type=1')">《用户协议》</text>和<text @click="jumpTo('/pages/setting/aboutUs?type=2')">《隐私政策》</text>
    </view>
  </view>
</template>

<script>
import loginBg from "../../static/login/loginBg.png";
import { ENV } from "../../.env.js";
import { isWeChatBrowser, throttleFunc } from "@/common/util";
import { getOpenId, getUserInfo, saveInviteCode } from '@/api/index'

export default {
  data() {
    return {
      loginBg,
      agreement: [],
    };
  },
  onLoad() {
    const that = this
    let url = window.location.href
    if (url.indexOf('code=') !== -1) {
      let code = url.split('code=')[1].split('&')[0]
      uni.showLoading({ mask: true })
      getOpenId({ code: code }).then(async (res) => {
        const token = 'Bearer ' + res.data.access_token
			  uni.setStorageSync('token', token)
        getUserInfo({}).then(async res => {
          uni.hideLoading()
			    uni.setStorageSync('userInfo', {...res.user, roles: res.roles})
          if(uni.getStorageSync('invitationCode') && !res.user.invitationName) {
            const parameter = {
              userId: res.user.userId,
              invitationCode: uni.getStorageSync('invitationCode'),
            }
            saveInviteCode(parameter)
            uni.removeStorageSync('invitationCode')
          }
          if(uni.getStorageSync('invitationUrl')) {
            that.reLaunch(uni.getStorageSync('invitationUrl'))
          } else if(res.user.phonenumber) {
            uni.showToast({
              icon: 'none',
              title: `登录成功`
            })
            setTimeout(() => {
              that.reLaunch('/pages/index/index')
            }, 1000)
          } else {
            uni.showToast({
              icon: 'none',
              title: `请绑定手机号`
            })
            setTimeout(() => {
              that.redirectTo('/pages/userInfo/userPhone')
            }, 1000)
          }
        })
      })
    }
  },
  methods: {
    wxLogin: throttleFunc(function() {
      if(this.agreement.length == 0) {
        uni.showToast({
          title: '请先勾选用户协议、隐私协议',
          icon: 'none'
        })
        return
      }
      // 判断是否是微信浏览器
      if (isWeChatBrowser()) {
        // 获取微信用户信息
        let url = ''
        if(ENV.BASE_API == '/api') {
          url = this.httpUrl
        } else {
          url = window.location.href
        }
        const appid = ENV.APP_ID
        let currentURL = encodeURIComponent(url)
        let redirect_uri = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${currentURL}&response_type=code&scope=snsapi_userinfo&forcePopup=true&state=STATE#wechat_redirect`
        window.location.href = redirect_uri
      } else {
        uni.showToast({
          icon: 'none',
          title: `请用微信打开网址`
        })
      }
    })
  },
};
</script>

<style lang="scss" scoped>
.page {
  width: 750rpx;
  min-height: 100vh;
  background: #f4f7f6;
  position: relative;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: 750rpx 100vh;
  display: flex;
  flex-direction: column;
  .login-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 654rpx;
    height: 100rpx;
    font-size: 32rpx;
    color: #FFFFFF;
    border-radius: 200rpx;
  }
  .agreement {
    display: flex;
    align-items: center;
    padding: 0 48rpx;
    padding-bottom: 106rpx;
    font-size: 28rpx;
    color: #000000;
    text {
      color: #53be07;
    }
  }
}
</style>
